package com.example.weather.city_manager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.weather.MainActivity;
import com.example.weather.R;
import com.example.weather.data.City;
import com.example.weather.data.CityDao;
import com.example.weather.data.WeatherDatabase;


import java.util.ArrayList;
import java.util.List;

public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView addIv,backIv,deleteIv;
    ListView cityLv;
    List<String> mDatas=new ArrayList<>();//列表数据源
    String[]hotCitys = {"北京","太原","沈阳","重庆","天津","南宁","杭州","武汉","烟台","西安"};
    private WeatherDatabase weatherDatabase;//数据库
    private CityDao cityDao;//城市接口
    private CityManagerAdapter adapter;//适配器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manager);
        //初始化
        cityLv = findViewById(R.id.city_lv);
        backIv = findViewById(R.id.city_iv_back);//跳转为主页面
        deleteIv = findViewById(R.id.city_iv_delete);//跳转为删除城市页面
        addIv=findViewById(R.id.city_iv_add);//跳转回搜索城市页面
        //得到数据库跟接口
        weatherDatabase=WeatherDatabase.getDatabase(this);
        cityDao=weatherDatabase.getCityDao();
        //获取数据库的数据
        mDatas=getData();
       //  添加点击监听事件
        addIv.setOnClickListener(this);
        deleteIv.setOnClickListener(this);
        backIv.setOnClickListener(this);
        adapter=new CityManagerAdapter(this,mDatas);//创建适配器
        cityLv.setAdapter(adapter);//设立适配器
    }
    @Override
    protected void onResume() {//退出页面时的执行操作
        super.onResume();//继承父类的方法
        mDatas.clear();//先清空数据
        mDatas=getData();//得到更新后的数据
        adapter=new CityManagerAdapter(this,mDatas);//创建新的适配器
        cityLv.setAdapter(adapter);//增加适配器
        adapter.notifyDataSetChanged();//提示适配器更新
    }
    private List<String>getData(){//获取数据源
        List cityList=new ArrayList<String>();
        List<City>cities=cityDao.findAll();//查找所有的城市
        if(cities.isEmpty()) {//如果为空
            for(String tmp_name:hotCitys){
                cityDao.insetCity(new City(tmp_name));//添加城市
                System.out.println("插入了城市"+tmp_name+"\n");
            }
            cities=cityDao.findAll();//再次查找所有
        }
        for (City c : cities) {
            cityList.add(c.getName());
            System.out.println(c);
        }
        return cityList;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.city_iv_add://跳转添加界面
                Intent intent_add=new Intent(this, SearchCityActivity.class);
                startActivity(intent_add);
                System.out.println("从城市管理界面跳转到搜索界面"+"\n");
                break;
            case R.id.city_iv_back:
                Intent intent_back=new Intent(this, MainActivity.class);
                startActivity(intent_back);
                System.out.println("从城市管理界面返回主页面"+"\n");
                break;
            case R.id.city_iv_delete:
                Intent intent_delete=new Intent(this, DeleteCityActivity.class);
                startActivity(intent_delete);
                System.out.println("从城市管理界面跳转到删除城市页面"+"\n");
                break;
        }
    }

}